package com.cmc.cloud.cmclink.doc.api.document;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.api.document.emums.AttachmentCodeEnum;
import com.cmc.cloud.cmclink.doc.api.document.rep.AttachmentBaseRespDto;
import com.cmc.cloud.cmclink.doc.api.document.rep.AttachmentTypeBaseDto;
import com.cmc.cloud.cmclink.doc.convert.AttachmentTypeConvert;
import com.cmc.cloud.cmclink.doc.entity.AttachmentTypeDO;
import com.cmc.cloud.cmclink.doc.mapper.AttachmentTypeMapper;
import com.cmc.cloud.cmclink.doc.service.si.BlnoService;
import com.cmc.cloud.cmclink.doc.service.so.AttachmentService;
import com.cmc.cloud.cmclink.doc.vo.blnovo.operatevo.BlnoOperateReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.attachmentvo.AttachmentCreateReqVO;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 附件类型查询
 *
 * @author lzp
 */
@RestController
public class AttachmentTypeBaseApiImpl implements AttachmentTypeBaseApi {

    @Resource
    private AttachmentTypeMapper attachmentTypeMapper;
    @Resource
    private AttachmentService attachmentService;
    @Resource
    private BlnoService blnoService;

    @Override
    public CommonResult<List<AttachmentTypeBaseDto>> queryAttachmentType(String useModules,String useFunctions) {
        LambdaQueryWrapperX<AttachmentTypeDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.eqIfPresent(AttachmentTypeDO::getUseModules, useModules)
                .eqIfPresent(AttachmentTypeDO::getUseFunctions,useFunctions);
        List<AttachmentTypeDO> attachmentTypeDOList = attachmentTypeMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(attachmentTypeDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<AttachmentTypeBaseDto> attachmentTypeBaseDtos = AttachmentTypeConvert.INSTANCE.convertAttachmentTypeBaseDto(attachmentTypeDOList);
        return CommonResult.success(attachmentTypeBaseDtos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Void> uploadAttachment(List<AttachmentBaseRespDto> createReqVOList) {
        attachmentService.createBatch(BeanUtil.copyToList(createReqVOList,AttachmentCreateReqVO.class));
        return CommonResult.success();
    }
}
